<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>04.control_flow | KnockOut</title>
  <script src="../assets/knockout-3.5.1.js"></script>
</head>
<body>

  <h4>不传参: 简洁语法</h4>
  <div data-bind='component: "message-editor"'></div>
  <message-editor></message-editor>

  <h4>传参: 完全语法，包含 name 和 params</h4>
  <div data-bind='component: {
    name: "message-editor",
    params: { initialText: "Hello, world!" }
  }'></div>
  <message-editor params="initialText: 'Hello, world!'"></message-editor>

  <script type="text/javascript">
    // 注册组件
    ko.components.register('message-editor', {
      // vm
      viewModel: function(params) {
        this.text = ko.observable(params && params.initialText || '');
      },
      // 模板
      template: '消息: <input data-bind="value: text" /> '
              + '(length: <span data-bind="text: text().length"></span>)'
    });
    
    ko.applyBindings();
  </script>

</body>
</html>